﻿//Copyright (c) 2010 William Liu, Enoch Ye
//William Liu <william.lyz@gmail.com>
//Enoch Ye <moonlight.enoch@gmail.com>

//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at

//       http://www.apache.org/licenses/LICENSE-2.0

//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using GBG_DBM.Support.DAL;
using GBG_DBM.GlobalParameters;

namespace GBG_DBM.Support.BLL
{
    /// <summary>
    /// This class is using to backup the database
    /// backup path example: Current_App_path/Backup/Year/Month/Date/Database_HH_mm_ss.accdb
    /// </summary>
    class Backup
    {
        #region FILEDS

        private string _backupPath;
        //private string _fileType;
        private string _pathByYear;
        private string _pathByMonth;
        private string _pathByDay;
        private string _fileNameByTime;
        private string _newFileName;
        private string _orgFileName;
        private string _currentDepth;
        //private string _expectDepth;
        private bool _isBackupPass;

        #endregion

        #region PROPERTIES
        #endregion

        #region CONSTRUCTORS

        public Backup(string BackupPath)
        {
            _backupPath = BackupPath;
        }

        #endregion

        #region METHODS

        //Abort this method, because application will run backup method before all the functions
        ////backup method, it will return three states:
        ////0: Unknow
        ////1: success
        ////-1: fail
        //public int BackupFile()
        //{
        //    if (DBHelper.GetDBConnection.State == System.Data.ConnectionState.Closed)
        //    {
        //        return 1;
        //    }
        //    return 0;
        //}

        //new backup method, it will return a bool
        public bool BackupFile()
        {
            _pathByYear = DateTime.Now.Year.ToString();
            _pathByMonth = DateTime.Now.Month.ToString();
            _pathByDay = DateTime.Now.Day.ToString();
            _fileNameByTime = DateTime.Now.ToString("HH_mm_ss");
            _backupPath = GlobalParams.DefaultBackupPath();
            _isBackupPass = false;

            //current depth means today's depth
            //for example: today is 2011/10/28, so the directory depth should be ./Backup/2011/10/28
            //so application will check current depth first then create relevant directory
            _currentDepth = _backupPath + "\\" + _pathByYear + "\\" + _pathByMonth + "\\" + _pathByDay;
           
            _newFileName = _currentDepth + "\\" + "Databse_" + _fileNameByTime + ".accdb";
            _orgFileName = GlobalParams.DBPath;

            //check directories and copy database to backup folder
            if (Directory.Exists(_currentDepth))
            {
                try
                {
                    File.Copy(_orgFileName, _newFileName, false);
                    _isBackupPass = true;
                }
                catch (Exception _Err)
                {
                    MessageBox.Show("Backup Error!\nReason:" + _Err.Message.ToString(), "File Copying Error",
                                                               MessageBoxButtons.OK,
                                                               MessageBoxIcon.Error);
                    _isBackupPass = false;
                }
            }
            else
            {
                try
                {
                    Directory.CreateDirectory(_currentDepth);
                    File.Copy(_orgFileName, _newFileName, false);
                    _isBackupPass = true;
                }
                catch (Exception _Ex)
                {
                    MessageBox.Show("Directory Error!\nReason:" + _Ex.Message.ToString(), "Create Directory Error",
                                                               MessageBoxButtons.OK,
                                                               MessageBoxIcon.Error);
                    return _isBackupPass = false;
                }
            }
            return _isBackupPass;
        }

        //this method is using to check the directories
        //if the folder does not exist, it will chreate one
        //this method should have better solution
        //abort this solution
        //
        //private bool DirCheck()
        //{
        //    bool _depthCheck = false;

        //    if (Directory.Exists(_backupPath))
        //    {
        //        #region byYear
        //        foreach (string _subYearDirs in Directory.GetDirectories(_backupPath))
        //        {
        //            if (Directory.Exists(_subYearDirs))
        //            {
        //                #region byMonth
        //                foreach (string _subMonthDirs in Directory.GetDirectories(_backupPath + "\\" + _pathByYear))
        //                {
        //                    if (Directory.Exists(_subMonthDirs))
        //                    {
        //                        #region byDate
        //                        foreach (string _subDateDirs in Directory.GetDirectories(_backupPath + "\\" + _pathByYear + "\\" + _pathByMonth))
        //                        {
        //                            if (Directory.Exists(_subDateDirs))
        //                            {
        //                                return _depthCheck = true;
        //                            }
        //                            else
        //                            {
        //                                try
        //                                {
        //                                    Directory.CreateDirectory(_pathByDate);
        //                                    return _depthCheck = true;
        //                                }
        //                                catch (Exception _Err)
        //                                {
        //                                    MessageBox.Show("Backup Error!\nReason:" + _Err.Message.ToString() + "\nLocation: Date Directory level.", "Error",
        //                                        MessageBoxButtons.OK,
        //                                        MessageBoxIcon.Error);
        //                                    return _depthCheck = false;
        //                                }
        //                            }
        //                        }
        //                        #endregion
        //                    }
        //                    else
        //                    {
        //                        try
        //                        {
        //                            Directory.CreateDirectory(_pathByMonth);
        //                            return _depthCheck = true;
        //                        }
        //                        catch (Exception _Err)
        //                        {
        //                            MessageBox.Show("Backup Error!\nReason:" + _Err.Message.ToString() + "\nLocation: Month Directory level.", "Error",
        //                                MessageBoxButtons.OK,
        //                                MessageBoxIcon.Error);
        //                            return _depthCheck = false;
        //                        }
        //                    }
        //                }
        //                #endregion
        //            }
        //            else
        //            {
        //                try
        //                {
        //                    Directory.CreateDirectory(_pathByYear);
        //                    return _depthCheck = true;
        //                }
        //                catch (Exception _Err)
        //                {
        //                    MessageBox.Show("Backup Error!\nReason:" + _Err.Message.ToString() + "\nLocation: Year Directory level.", "Error",
        //                        MessageBoxButtons.OK,
        //                        MessageBoxIcon.Error);
        //                    return _depthCheck = false;
        //                }
        //            }
        //        }
        //        #endregion
        //    }
        //    else
        //    {
        //        try
        //        {
        //            Directory.CreateDirectory(_backupPath);
        //        }
        //        catch (Exception _Err)
        //        {
        //            MessageBox.Show("Backup Error!\nReason:" + _Err.Message.ToString() + "\nLocation: Root Backup Directory.", "Error",
        //                                        MessageBoxButtons.OK,
        //                                        MessageBoxIcon.Error);
        //            return _depthCheck = false;
        //        }
        //    }
           
        //    return _depthCheck;
        //}

        #endregion

    }
}
